package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntNjsjBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntNjsjBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntNjsjBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntNjsjBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntNjsjBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土凝结时间试验检测记录详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntNjsjBaDetailServiceImpl implements IReportSnhntNjsjBaDetailService {

    private final ReportSnhntNjsjBaDetailMapper baseMapper;

    /**
     * 查询混凝土凝结时间试验检测记录详情
     */
    @Override
    public ReportSnhntNjsjBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询混凝土凝结时间试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntNjsjBaDetailVo> queryPageList(ReportSnhntNjsjBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntNjsjBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntNjsjBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土凝结时间试验检测记录详情列表
     */
    @Override
    public List<ReportSnhntNjsjBaDetailVo> queryList(ReportSnhntNjsjBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntNjsjBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntNjsjBaDetail> buildQueryWrapper(ReportSnhntNjsjBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntNjsjBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntNjsjBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBianh() != null, ReportSnhntNjsjBaDetail::getBianh, bo.getBianh());
        lqw.eq(bo.getCessj1() != null, ReportSnhntNjsjBaDetail::getCessj1, bo.getCessj1());
        lqw.eq(bo.getCessj2() != null, ReportSnhntNjsjBaDetail::getCessj2, bo.getCessj2());
        lqw.eq(bo.getCessj3() != null, ReportSnhntNjsjBaDetail::getCessj3, bo.getCessj3());
        lqw.eq(bo.getLis1() != null, ReportSnhntNjsjBaDetail::getLis1, bo.getLis1());
        lqw.eq(bo.getLis2() != null, ReportSnhntNjsjBaDetail::getLis2, bo.getLis2());
        lqw.eq(bo.getLis3() != null, ReportSnhntNjsjBaDetail::getLis3, bo.getLis3());
        lqw.eq(bo.getCezjmj1() != null, ReportSnhntNjsjBaDetail::getCezjmj1, bo.getCezjmj1());
        lqw.eq(bo.getCezjmj2() != null, ReportSnhntNjsjBaDetail::getCezjmj2, bo.getCezjmj2());
        lqw.eq(bo.getCezjmj3() != null, ReportSnhntNjsjBaDetail::getCezjmj3, bo.getCezjmj3());
        lqw.eq(bo.getGuanryl1() != null, ReportSnhntNjsjBaDetail::getGuanryl1, bo.getGuanryl1());
        lqw.eq(bo.getGuanryl2() != null, ReportSnhntNjsjBaDetail::getGuanryl2, bo.getGuanryl2());
        lqw.eq(bo.getGuanryl3() != null, ReportSnhntNjsjBaDetail::getGuanryl3, bo.getGuanryl3());
        lqw.eq(bo.getGuanrzl1() != null, ReportSnhntNjsjBaDetail::getGuanrzl1, bo.getGuanrzl1());
        lqw.eq(bo.getGuanrzl2() != null, ReportSnhntNjsjBaDetail::getGuanrzl2, bo.getGuanrzl2());
        lqw.eq(bo.getGuanrzl3() != null, ReportSnhntNjsjBaDetail::getGuanrzl3, bo.getGuanrzl3());
        return lqw;
    }

    /**
     * 新增混凝土凝结时间试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntNjsjBaDetailBo bo) {
        ReportSnhntNjsjBaDetail add = BeanUtil.toBean(bo, ReportSnhntNjsjBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改混凝土凝结时间试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntNjsjBaDetailBo bo) {
        ReportSnhntNjsjBaDetail update = BeanUtil.toBean(bo, ReportSnhntNjsjBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntNjsjBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土凝结时间试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
